/*
 * @Author: daniel_du dll_2018@126.com
 * @Date: 2022-05-08 12:50:37
 * @LastEditors: daniel_du dll_2018@126.com
 * @LastEditTime: 2022-05-09 16:55:52
 * @FilePath: /safe_transmission_platform/src/mysqldb.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "mysqldb.h"
// #include <sstream>
#include "log.h"

static sylar::Logger::ptr g_logger=DANIEL_LOG_NAME("system");

namespace daniel
{
    MysqlDB::MysqlDB(const MysqlInfo& info):m_info(info)
    {
    }
    MysqlDB::~MysqlDB()
    {
        if (m_connectSql){
            mysql_close(m_connectSql);
            delete m_connectSql;
            m_connectSql=nullptr;
        }
    }   
    bool MysqlDB::connectToDB()
    {
        m_connectSql=(MYSQL*)malloc(sizeof(MYSQL));
        mysql_init(m_connectSql);
        if (!mysql_real_connect(m_connectSql,m_info.m_host.data(),m_info.m_user.data(),m_info.m_pwd.data(),m_info.m_database.data(),m_info.m_port,nullptr,0)){
            DANIEL_ASSERT2(false,"connect to sql failed!");
            return false;
        }
        return true;
    }
    //执行select查询  并且以字符串显示结果
    MysqlDB::queryResult MysqlDB::excuteQuery(const std::string& query)
    {

        int ret=mysql_query(m_connectSql,query.c_str());
        DANIEL_ASSERT2(ret==0,"excute sql failed!");
        // DANIEL_LOG_DEBUG(g_logger)<<"ret:"<<ret<<std::endl;
        MYSQL_RES* queryResult=nullptr;
        queryResult=mysql_store_result(m_connectSql);
        // int row_count=mysql_num_rows(queryResult);
        int field_count=0;
        MYSQL_ROW row=nullptr;
        if (queryResult!=nullptr){
            field_count=mysql_num_fields(queryResult);
            row=mysql_fetch_row(queryResult);
        }
        MysqlDB::queryResult result;
        // std::stringstream ss;
        //row是储存结果的链表，储存的是每一行的结果   
        while(row!=nullptr){
            std::vector<std::string> rowData;
            for(int i=0;i<field_count;i++){
                if (row[i]!=nullptr){
                    rowData.push_back(row[i]);
                }//当某个字段数据为空时，返回的数据是
                else {
                    rowData.push_back("");
                }
            }
            result.push_back(rowData);
            row=mysql_fetch_row(queryResult);
        }
        mysql_free_result(queryResult);
        return result;
    }
    //执行DML语言，通常没有返回值，所以用上面的函数会段错误，所以重写
    bool MysqlDB::excuteDML(const std::string& query)
    {
        int ret=mysql_query(m_connectSql,query.c_str());
        DANIEL_ASSERT2(!ret,"excute sql DML failed!");
        bool retBool=(ret==0?true:false);
        return retBool;
    }
    void MysqlDB::PrintResult(const MysqlDB::queryResult& result)
    {
        std::cout<<std::string(40,'*')<<"Result:"<<std::string(40,'*')<<std::endl;   
        for(const auto& item:result){
            for(const auto& item2:item){
                std::cout<<item2<<"\t";
            }
            std::cout<<std::endl;
        }
        std::cout<<std::string(40,'*')<<"--End--"<<std::string(40,'*')<<std::endl;   

    }



} // namespace daniel


